﻿//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Exception Handling Application Block
//===============================================================================

using System;
using System.ComponentModel;
using System.Drawing.Design;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Configuration.Design;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration;
using Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Configuration.Design;
using EntLibContrib.ExceptionHandling.Configuration.Design.Properties;

namespace EntLibContrib.ExceptionHandling.Configuration.Design
{
	/// <summary>
	/// Represents a <see cref="SqlExceptionWrapHandlerData"/> configuration element. 
	/// </summary>
	public class SqlExceptionWrapHandlerNode : ExceptionHandlerNode
	{
		#region Fields
		private ErrorCodeNotConfiguredBehavior errorCodeNotConfiguredBehavior;
		private string defaultExceptionMessage;
		private string defaultWrapExceptionTypeName;
		#endregion

		#region Construction
		/// <summary>
		/// Initialize a new instance of the <see cref="SqlExceptionWrapHandlerNode"/> class.
		/// </summary>
		public SqlExceptionWrapHandlerNode()
			: this(new SqlExceptionWrapHandlerData(
					Resources.SqlExceptionWrapHandlerNodeName,
					ErrorCodeNotConfiguredBehavior.WrapWithDefaultException,
					"",
					"System.Exception",
					new NamedElementCollection<ErrorCodeEntryData>()))
		{
		}

		/// <summary>
		/// Initialize a new instance of the <see cref="SqlExceptionWrapHandlerNode"/> class with a <see cref="SqlExceptionWrapHandlerData"/> instance.
		/// </summary>
		/// <param name="data">A <see cref="SqlExceptionWrapHandlerData"/> instance</param>
		public SqlExceptionWrapHandlerNode(SqlExceptionWrapHandlerData data)
		{
			if (null == data)
				throw new ArgumentNullException("data");

			Rename(data.Name);
			this.defaultExceptionMessage = data.DefaultExceptionMessage;
			this.defaultWrapExceptionTypeName = data.DefaultWrapExceptionTypeName;
			foreach (ErrorCodeEntryData errorCode in data.ErrorCodes)
			{
				ErrorCodeEntryNode errorNode = new ErrorCodeEntryNode(errorCode);
				this.AddNode(errorNode);
			}
		}
		#endregion

		#region Properties
		/// <summary>
		/// Gets the <see cref="SqlExceptionWrapHandlerData"/> this node represents.
		/// </summary>
		/// <value>
		/// The <see cref="SqlExceptionWrapHandlerData"/> this node represents.
		/// </value>
		[Browsable(false)]
		public override ExceptionHandlerData ExceptionHandlerData
		{
			get
			{
				SqlExceptionWrapHandlerData data = new EntLibContrib.ExceptionHandling.Configuration.SqlExceptionWrapHandlerData(
					this.Name,
					this.errorCodeNotConfiguredBehavior,
					this.defaultExceptionMessage,
					this.defaultWrapExceptionTypeName,
					new NamedElementCollection<ErrorCodeEntryData>());

				foreach (ErrorCodeEntryNode errorCodeNode in this.Nodes)
				{
					data.ErrorCodes.Add(errorCodeNode.ErrorCodeEntry);
				}
				return data;
			}
		}

		/// <summary>
		/// Gets or sets the error code not configured behavior.
		/// </summary>
		/// <value>The error code not configured behavior.</value>
		[SRDescription("ErrorCodeNotConfiguredBehaviorDescription", typeof(Resources))]
		[SRCategory("CategoryGeneral", typeof(Resources))]
		public ErrorCodeNotConfiguredBehavior ErrorCodeNotConfiguredBehavior
		{
			get { return this.errorCodeNotConfiguredBehavior; }
			set { this.errorCodeNotConfiguredBehavior = value; }
		}

		/// <summary>
		/// Gets or sets the default exception message.
		/// </summary>
		/// <value>The default exception message.</value>
		[SRDescription("DefaultExceptionMessageDescription", typeof(Resources))]
		[SRCategory("CategoryGeneral", typeof(Resources))]
		public string DefaultExceptionMessage
		{
			get { return this.defaultExceptionMessage; }
			set { this.defaultExceptionMessage = value; }
		}

		/// <summary>
		/// Gets or sets the default name of the exception type.
		/// </summary>
		/// <value>The default name of the exception type.</value>
		[SRCategory("CategoryGeneral", typeof(Resources))]
		[Editor(typeof(TypeSelectorEditor), typeof(UITypeEditor))]
		[BaseType(typeof(Exception), TypeSelectorIncludes.BaseType)]
		public string DefaultExceptionTypeName
		{
			get { return this.defaultWrapExceptionTypeName; }
			set { this.defaultWrapExceptionTypeName = value; }
		}
		#endregion

		#region Protected Methods
		/// <summary>
		/// Releases the unmanaged resources used by the <see cref="SqlExceptionWrapHandlerNode"/> and optionally releases the managed resources.
		/// </summary>
		/// <param name="disposing"><see langword="true"/> to release both managed and unmanaged resources; <see langword="false"/> to release only unmanaged resources.</param>
		protected override void Dispose(bool disposing)
		{
			if (disposing)
			{
			}
			base.Dispose(disposing);
		}
		#endregion
	}
}
